package com.mofeng.myshop.controller.portal;


import com.mofeng.myshop.domain.dto.OrderFromDto;
import com.mofeng.myshop.domain.dto.search.OrderFromSearchDto;
import com.mofeng.myshop.service.IOrderFromService;
import com.mofeng.myshop.until.Constants;
import com.mofeng.myshop.until.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author MoFeng
 * @since 2024-04-14
 */
@RestController
@RequestMapping("/portal/order")
@RequiredArgsConstructor
@Tag(name = "订单接口", description = "类介绍")
public class OrderFromController {

    private final IOrderFromService orderFromService;

    @Operation(summary = "商品添加到购物车")
    @PostMapping("/add")
    @PreAuthorize(value = "@permissionService.NORMAL()")
    public R addToShopping(@RequestBody OrderFromDto orderFromDto) {
        orderFromDto.setState(Constants.Order.SAVE);
        return orderFromService.addToShopping(orderFromDto);
    }

    @Operation(summary = "购物车商品修改")
    @PutMapping("")
    @PreAuthorize(value = "@permissionService.NORMAL()")
    public R updateShopping(@RequestBody OrderFromDto orderFromDto) {
        return orderFromService.updateShopping(orderFromDto);
    }

    @Operation(summary = "直接购买商品")
    @PostMapping("/buy")
    @PreAuthorize(value = "@permissionService.NORMAL()")
    public R buy(@RequestBody OrderFromDto orderFromDto) {
        return orderFromService.buy(orderFromDto);
    }

    @Operation(summary = "重新发货")
    @PostMapping("/reSend")
    @PreAuthorize(value = "@permissionService.NORMAL()")
    public R reSend(@RequestBody OrderFromDto orderFromDto) {
        return orderFromService.reBuy(orderFromDto.getId());
    }

    @Operation(summary = "购物车购买商品")
    @PostMapping("/buy/list")
    @PreAuthorize(value = "@permissionService.NORMAL()")
    public R buyList(@RequestBody List<OrderFromDto> orderFromDtoList) {
        return orderFromService.buyList(orderFromDtoList);
    }

    @Operation(summary = "删除订单信息")
    @DeleteMapping("")
    @PreAuthorize(value = "@permissionService.NORMAL()")
    public R delList(@RequestParam("ids") List<String> ids) {
        return orderFromService.delPortal(ids);
    }

    @Operation(summary = "查看商品列表")
    @GetMapping("/list/{page}/{size}")
    public R getList(@PathVariable(value = "page") int page,
                     @PathVariable(value = "size") int size,
                     @RequestParam(value = "state", required = false) String state,
                     @RequestParam(value = "keyword", required = false) String keyword,
                     @RequestParam(value = "projectId", required = false) Long projectId,
                     @RequestParam(value = "categoryId", required = false) Long categoryId,
                     @RequestParam(value = "startTime", required = false) String startTime,
                     @RequestParam(value = "endTime", required = false) String endTime) {
        OrderFromSearchDto orderFromSearchDto = new OrderFromSearchDto();
        orderFromSearchDto.setPage(page);
        orderFromSearchDto.setSize(size);
        orderFromSearchDto.setState(state);
        orderFromSearchDto.setKeyword(keyword);
        orderFromSearchDto.setStart(startTime);
        orderFromSearchDto.setCategoryId(categoryId);
        orderFromSearchDto.setProjectId(projectId);
        orderFromSearchDto.setEnd(endTime);
        return orderFromService.getListPortal(orderFromSearchDto);
    }

}
